import { addEN } from '@/mustache/customize.js'
import Mustache from 'mustache'
import { regStrToNum } from '@/utils/LodashUtils.js'
import GC from '@grapecity/spread-sheets'

export const ingredientListSuper = (data, caller) => {

  const wrapAndAutoFit = (r, c) => {
    data.tempSheet.getCell(r, c).wordWrap(true)
    data.tempSheet.setRowHeight(r, 100)
  }

  //TODO: 放到Promise.all() 那里完成最后的每列求值逻辑, 因为要先把单元格位置腾出来后, 才能进行求值操作
  const recordSet = new Set()
  const record = (caller) => {
    recordSet.add(caller)
  }

  return (text, render) => {
    addEN(Mustache.parse(text)[0][1])
    if (!recordSet.has(caller)) {
      for (let i = 1; i < data.ingredients.length; i++) {
        console.log(data.curRC)
        console.log('i', data.curRC.r + i)
        console.log('i.value', data.tempSheet.getValue(data.curRC.r + 1, data.curRC.c))
        if (!data.tempSheet.getValue(data.curRC.r + i, data.curRC.c)
          // && i + 1 !== data.ingredients.length) {
        ){
          console.log('copy and paste')
          data.tempSheet.addRows(data.curRC.r + i + 1, 1)
          data.tempSheet.copyTo(
            data.curRC.r, data.curRC.c,
            data.curRC.r + i + 1, data.curRc.c,
            1, data.tempSheet.getColumnCount(),
            GC.Spread.Sheets.CopyToOptions.all
          )
        }
      }
      record(caller)
    }

    for (let i = 0; i < data.ingredients.length; i++) {
      data.curIng = data.ingredients[i]
      const result = render(text)
      data.tempSheet.setValue(data.curRC.r + i, data.curRC.c, regStrToNum(result))
      wrapAndAutoFit(data.curRC.r + i, data.curRC.c)

    }
  }

}
